package com.xh3140.xwidget.dialog.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xh3140.xwidget.dialog.adapter.ListItemAdapter;
import com.xh3140.xwidget.dialog.params.ListParams;

import java.util.List;

public class ListView extends RecyclerView {

  private final ListItemAdapter mListAdapter;

  public ListView(@NonNull Context context) {
    super(context);
    setHasFixedSize(true);
    setOverScrollMode(OVER_SCROLL_NEVER);
    setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
    setLayoutParams(new RootView.LayoutParams(RootView.LayoutParams.MATCH_PARENT, RootView.LayoutParams.WRAP_CONTENT, 1f));
    mListAdapter = new ListItemAdapter();
    setAdapter(mListAdapter);
  }

  /**
   * 获取单选的选取位置索引
   * 注意：没有选中任何项目时返回负数(-1)
   */
  public int getCheckedIndex() {
    return mListAdapter.getCheckedIndex();
  }

  /**
   * 获取多选的选取位置索引列表
   * 注意：没有选中任何项目时返回空列表
   */
  @NonNull
  public List<Integer> getCheckedIndexList() {
    return mListAdapter.getCheckedIndexList();
  }

  /**
   * 配置视图
   *
   * @param params 列表主体视图配置属性
   */
  public ListView configView(@NonNull ListParams params) {
    if (params.hasDivider) {
      addItemDecoration(new ItemDivider(params.dividerColor, params.dividerThickness));
    }
    mListAdapter.setIsRadio(params.radio);
    mListAdapter.submitList(params.items);
    if (params.callback != null) {
      params.callback.callback(this);
    }
    return this;
  }

  /**
   * 分割线
   */
  private static class ItemDivider extends ItemDecoration {
    private final Drawable mDivider;
    private final Rect mBounds = new Rect();

    public ItemDivider(@ColorInt int color, int thickness) {
      final GradientDrawable drawable = new GradientDrawable();
      drawable.setColor(ColorStateList.valueOf(color));
      drawable.setSize(thickness, thickness);
      mDivider = drawable;
    }

    @Override
    public void onDraw(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull State state) {
      if (parent.getLayoutManager() == null) {
        return;
      }
      canvas.save();
      final int left, right;
      if (parent.getClipToPadding()) {
        left = parent.getPaddingLeft();
        right = parent.getWidth() - parent.getPaddingRight();
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();
        canvas.clipRect(left, top, right, bottom);
      } else {
        left = 0;
        right = parent.getWidth();
      }
      final int dividerCount = parent.getChildCount() - 1;
      for (int i = 0; i < dividerCount; i++) {
        final View child = parent.getChildAt(i);
        parent.getDecoratedBoundsWithMargins(child, mBounds);
        final int bottom = mBounds.bottom + Math.round(child.getTranslationY());
        final int top = bottom - mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(canvas);
      }
      canvas.restore();
    }
  }
}
